package Client;


import java.net.Socket;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*; 

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ReceiveCoversation extends JFrame implements Runnable, ActionListener {

	private Socket m_sock = null;
	private   PrintWriter out = null;
	private  BufferedReader in = null;
	
	private JTextField champIn;
	private JTextField champOut;
	private JButton jValider;
	
	public ReceiveCoversation(Socket cli){
		m_sock =cli;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(500,400);
		JPanel contentPane = new JPanel(new GridLayout(3,1));
		champIn = new JTextField();
		champOut = new JTextField();
		jValider = new JButton("valider");
		jValider.addActionListener(this);
		contentPane.add(champIn);
		contentPane.add(champOut);
		contentPane.add(jValider);
		setContentPane(contentPane);
		this.setVisible(true);
	}
	
	public void run(){
		try{
			in = new BufferedReader(new InputStreamReader(m_sock.getInputStream()));
			out = new PrintWriter(m_sock.getOutputStream());
			Thread t3 = new Thread(new Reception(in,champIn));
			t3.start();
			/*
			Thread t4 = new Thread(new Emission(out));
			t4.start();
			**/
		} catch (Exception e){
			System.err.println(e);
		}
	}

	
	public void actionPerformed(ActionEvent arg) {
		String message =null;
		Object source = arg.getSource();
		if(source == jValider){
			//jReceive.setText(jSend.getText());
			message = "Votre message :";
			message += champOut.getText();
			out.println(message);
			out.flush();
		}	
		
	}
	
}// fin classe